package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrType;

public class ARecordRow extends AxdrType {
    private byte[] dataCode = null;
    private SubSeqOfDatas data = null;
    private int length = 0;

    public byte[] getDataCode() {
        return dataCode;
    }

    public SubSeqOfDatas getData() {
        return data;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setData(SubSeqOfDatas data) {
        this.data = data;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        this.data = new SubSeqOfDatas();
        // Assuming setLength is a method that sets the length of the sequence
        this.data.setLength(this.length);
        codeLength += this.data.decode(input);
        return codeLength;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        int codeLength = 0;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (int i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if (this.data != null) {
                codeLength += this.data.encode(output);
            } else {
                throw new RuntimeException("data is null");
            }
        }
        logEncodeMsg("ARecordRow",output);
        return codeLength;
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.toByteArray();
    }

    @Override
    public String toString() {
        if (this.data != null) {
            return "sequence: {" + "data: " + this.data + "}";
        } else {
            return "data is null";
        }
    }
}
